//  Created by 莫瑞权 on 2022/5/9.
//  Copyright © 2022 Talkfun. All rights reserved.
//

import UIKit

public class TF1to16VideoAreaCell: UICollectionViewCell {
    //缩放按钮
    @objc public let zoomButton = UIButton()
    //做背景透明
    @objc public  let scoreBackgroundColor = UIView()
    //关闭音量
    @objc public  let audioStatus = UIImageView()
    @objc public  let videoStatus = UIImageView()
    //音频 视频 关闭了
    @objc public  let multimediaShutdown = TFMultimediaShutdown()
    @objc public var model:TalkfunRtcModel?
    {
        didSet{
            if(model != nil)
            {
                self.modelUI(model!)
            }
        }
    }
    @objc public weak var delegate : TF1to16VideoAreaCellDelegate?
    //保存全屏前的位置
    var tempFrame = CGRect(x: 0, y: 0, width: 0, height: 0)
    @objc public let video = UIView()
    @objc public var ViewWidth:CGFloat = 0
    @objc public var ViewHight:CGFloat = 0
    @objc public let nameContainer = UIView()
    @objc public let scoreButton = TFScoreButton()
 
    //显示奖杯 true:显示,false:隐藏
    @objc public var showCup:Bool = false
    let nameContainerBackgroundColor = UIView()
    let name = UILabel()
    let graffitiStatus = UIImageView()
   
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        let doubleTap = UITapGestureRecognizer.init(target: self, action: #selector(self.doubleTapAction))
        doubleTap.numberOfTapsRequired = 2
        self.addGestureRecognizer(doubleTap)
        
        self.layer.cornerRadius = 4
        self.clipsToBounds = false
            
        self.video.backgroundColor = UIColor.clear
        self.addSubview(self.video)
        //videoView

        self.video.clipsToBounds = true
        self.video.layer.cornerRadius = 4

        self.multimediaShutdown.clipsToBounds = true
        self.multimediaShutdown.layer.cornerRadius = 4
        self.multimediaShutdown.backgroundColor = UIColor.clear
        self.multimediaShutdown.isHidden = true
        self.addSubview(self.multimediaShutdown)

        //1.
        self.nameContainer.backgroundColor = UIColor.clear
        self.nameContainer.layer.cornerRadius = 3
        self.addSubview(self.nameContainer)

        //2.
        self.nameContainer.layer.cornerRadius = 3
        self.nameContainer.addSubview(self.nameContainerBackgroundColor)

        //3.
        self.name.backgroundColor = UIColor.clear
        self.name.numberOfLines = 0;
        self.nameContainer.addSubview(self.name)
        self.name.textColor = UIColor(red: 255/255.0, green: 255/255.0, blue: 255/255.0, alpha: 1)
        //  关闭音量
        self.audioStatus.backgroundColor = UIColor.clear
        self.audioStatus.image = UIImage.init(named: "HTLive.bundle/audioOff1vN")
        self.nameContainer.addSubview(self.audioStatus)
       
        self.audioStatus.isHidden = true
        //视频开关
       
        self.videoStatus.backgroundColor = UIColor.clear
        self.nameContainer.addSubview(self.videoStatus)
        self.videoStatus.image =  UIImage.init(named: "HTLive.bundle/item_otm_video")
        self.videoStatus.isHidden = true

        //涂鸦
        self.graffitiStatus.backgroundColor = UIColor.clear
        self.nameContainer.addSubview(self.graffitiStatus)
        self.graffitiStatus.image = UIImage.init(named: "HTLive.bundle/graffiti1vN")
        self.graffitiStatus.isHidden = true

        //奖励的背景
        self.addSubview(self.scoreBackgroundColor)

        //奖励 的 主容器
        self.scoreButton.setImage(UIImage.init(named: "HTLive.bundle/smalljiangbei"), for: .normal)
        self.scoreButton.backgroundColor = UIColor.clear
        self.addSubview(self.scoreButton)

        self.zoomButton.backgroundColor = UIColor(red: 13/255.0, green: 15/255.0, blue: 18/255.0, alpha: 0.6)
        self.zoomButton.isHidden = true
        self.addSubview(self.zoomButton)
        self.zoomButton.setImage(UIImage.init(named: "HTLive.bundle/shrink1vN"), for: .normal)
        self.zoomButton.addTarget(self, action: #selector(self.zoomButtonClick), for: .touchUpInside)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    public override func layoutSubviews() {
        super.layoutSubviews()
        
        self.zoomButton.frame = CGRect(x: self.frame.size.width-44-20, y: self.frame.size.height - 44 - 15, width: 44, height: 44)
       
        // 超出主层边框的都会被裁减调用
         self.zoomButton.layer.masksToBounds = true
        // 圆角半径  能把一个下正文体,改成圆形
         self.zoomButton.layer.cornerRadius = 22
    }
    
    func modelUI(_ model:TalkfunRtcModel)
    {
        
        let font = UIFont.PingFang(size: 11)
        
        if self.video.subviews.count>0{
           self.video.subviews.forEach({$0.removeFromSuperview()})
        }
    
        
        if ( model.isFull == false) {
            model.view.frame = CGRect(x: 0, y: 0, width: self.ViewWidth, height: self.ViewHight)
            self.video.addSubview(model.view)
        }
        //音频视频关闭
        var Shutdown:Bool = false
        if (self.multimediaShutdownUI(model)) {

        self.multimediaShutdown.gl.colors = [UIColor(red: 45/255.0, green: 52/255.0, blue: 59/255.0, alpha: 1.0).cgColor, UIColor(red: 45/255.0, green: 52/255.0, blue: 59/255.0, alpha: 1.0).cgColor]
        self.multimediaShutdown.gl.locations = [1,1]
            if (model.role == .roleSpadmin) {
                
                self.multimediaShutdown.imageView.image = UIImage.init(named: "HTLive.bundle/jiangshi")
            }else{
                self.multimediaShutdown.imageView.image = UIImage.init(named: "HTLive.bundle/xueyuan")
               
            }
        
        self.multimediaShutdown.isHidden = false
        self.videoStatus.isHidden = true
        self.audioStatus.isHidden = true
            
            Shutdown = true;
        }else{
            //视频关闭
            if (model.video == .closeForZHUBO || model.video == .closeForUser) {
                self.multimediaShutdown.gl.colors = [UIColor(red: 45/255.0, green: 52/255.0, blue: 59/255.0, alpha: 1.0).cgColor, UIColor(red: 45/255.0, green: 52/255.0, blue: 59/255.0, alpha: 1.0).cgColor]
                self.multimediaShutdown.gl.locations = [1,1]
                
                self.multimediaShutdown.imageView.image = UIImage.init(named: "HTLive.bundle/closevideo-yinliang")
        
                self.multimediaShutdown.isHidden = false
                
                self.videoStatus.isHidden = true
                self.audioStatus.isHidden = false
      
            }
            //视频打开
            else if ( model.video == .openForZHUBO || model.video == .openForUser) {
                self.videoStatus.isHidden = true
                self.multimediaShutdown.isHidden = true


            }
      
        }
        let stringStr = NSMutableAttributedString.getAttributed(string: model.nickname, font: font, foregroundColor: UIColor(red: 255/255.0, green: 255/255.0, blue: 255/255.0, alpha: 1))
     
        self.name.attributedText = stringStr
        //打开
        if (model.drawPower) {
            if (model.role != .roleSpadmin) {
                    self.graffitiStatus.isHidden = false
            }else{
                self.graffitiStatus.isHidden = true
            }
            
        }else{
            self.graffitiStatus.isHidden = true
        }

        var temp:NSInteger = model.score
                      temp = temp/2
        
        
        let score = String.init(format: "%ld", temp)
        
        self.scoreButton.setTitle(score, for: .normal)
      
        self.scoreButton.titleLabel!.font = font

        let newScore:String = "x" + score
        let attrsSize: CGSize = newScore.size(withAttributes: [NSAttributedString.Key.font: font])
        
         
            
            //19 * 0.6  j是图片宽高
            
        let scoreButtonW:CGFloat = 20 * 0.6 + attrsSize.width
            
            self.scoreBackgroundColor.frame = CGRect(x: 0, y: 0, width: scoreButtonW+(scoreButtonW*0.4), height: 20)

            self.scoreBackgroundColor.backgroundColor = UIColor(red: 2/255.0, green: 11/255.0, blue: 20/255.0, alpha: 0.31)
       
            self.scoreButton.frame = self.scoreBackgroundColor.frame
            
        let maskPath1 = UIBezierPath.init(roundedRect: self.scoreBackgroundColor.bounds, byRoundingCorners:[UIRectCorner.topRight,UIRectCorner.bottomRight], cornerRadii: CGSize(width: self.scoreBackgroundColor.frame.size.height/2, height: self.scoreBackgroundColor.frame.size.height/2))
   
        let maskLayer1 = CAShapeLayer()
        maskLayer1.frame = self.scoreBackgroundColor.bounds;
        maskLayer1.path = maskPath1.cgPath
        
        self.scoreBackgroundColor.layer.mask = maskLayer1
        
        self.video.frame = CGRect(x: 0, y: 0, width: self.ViewWidth, height: self.ViewHight)
        self.multimediaShutdown.frame = self.video.frame
            
        let nameContainerHeight:CGFloat = 17
        var audioWidth:CGFloat   = 17
        let audioHeight:CGFloat  = 17
        var videoStatusWidth:CGFloat = 10
        var graffitiStatusWidth:CGFloat = 10
            
        let nameHeight:CGFloat = audioWidth
        let nameX:CGFloat = 5

          
        let size: CGSize = model.nickname.size(withAttributes: [NSAttributedString.Key.font: font])
        
        
        var nameContainerWidth:CGFloat = 0

            //是老师
        if (model.role != .roleeUser) {

                    self.scoreButton.isHidden = true;
                    self.scoreBackgroundColor.isHidden = true;
                    self.nameContainerBackgroundColor.backgroundColor = UIColor(red: 23/255.0, green: 145/255.0, blue: 251/255.0, alpha: 0.8)
                
            }else{
                    //学员
                if (self.showCup) {
                    self.scoreButton.isHidden = false
                    self.scoreBackgroundColor.isHidden = false
                } else {
                    self.scoreButton.isHidden = true
                    self.scoreBackgroundColor.isHidden = true
                }
               
                    
                self.nameContainerBackgroundColor.backgroundColor = UIColor(red: 29/255.0, green: 34/255.0, blue: 41/255.0, alpha: 0.8)
            
                
            
            }
        
        //没有禁言与没有涂鸦的情况下
                nameContainerWidth = size.width+nameX+nameX
            
            //音频 关闭
        if (model.audio == .closeForZHUBO || model.audio == .closeForUser) {
                if (Shutdown == false) {
                    self.audioStatus.isHidden = false
                    nameContainerWidth = nameContainerWidth + audioWidth+nameX
                }
                
            }
            //音频打开
        else   if (model.audio == .openForZHUBO || model.audio == .openForUser) {
                self.audioStatus.isHidden = true

            }
        
        //TPDP:涂鸦权限
        if (model.drawPower&&model.role != .roleSpadmin) {
                    nameContainerWidth = nameContainerWidth + graffitiStatusWidth+nameX
                }
                
                
                
                //超出
                if (nameContainerWidth>self.ViewWidth) {
                    nameContainerWidth = self.ViewWidth;
                }
                
                self.nameContainer.frame = CGRect(x: 0, y: self.ViewHight - nameContainerHeight, width: nameContainerWidth, height: nameContainerHeight)
      
        
            self.nameContainerBackgroundColor.frame = CGRect(x: 0, y: 0, width: self.nameContainer.frame.size.width, height: self.nameContainer.frame.size.height)
      
            
        let audioY:CGFloat = (self.nameContainer.frame.size.height - audioHeight)/2;

            
            if (self.audioStatus.isHidden) {
                audioWidth = 0;

            }
            
        self.nameContainer.backgroundColor = UIColor.clear
        
        
        var  graffitiX =  self.nameContainer.frame.size.width
        //有涂鸦权限才减
        if (model.drawPower&&model.role != .roleSpadmin) {
                graffitiX  = graffitiX-graffitiStatusWidth-nameX;
        }
                
        self.graffitiStatus.frame = CGRect(x: graffitiX, y: (self.nameContainer.frame.size.height - graffitiStatusWidth)/2, width: graffitiStatusWidth, height: graffitiStatusWidth)
   
        var videoStatusSpacing:CGFloat = 6
            
            if (self.videoStatus.isHidden == true) {
                 videoStatusWidth = 0
                videoStatusSpacing = 0
            }
            
        var videoX:CGFloat = 0
            //隐藏了
            if (self.audioStatus.isHidden == true) {
                videoX = graffitiX - videoStatusWidth
            }else{
                videoX = self.audioStatus.frame.origin.x-videoStatusWidth-videoStatusSpacing
            }
            
            self.videoStatus.frame = CGRect(x: videoX, y: audioY, width: videoStatusWidth, height: videoStatusWidth)
     
            
            //视频隐藏了
            if (self.graffitiStatus.isHidden ) {

                graffitiStatusWidth = 0
            }
        
        let  audioStatusX = graffitiX - audioWidth - nameX
            
        self.audioStatus.frame = CGRect(x: audioStatusX, y: audioY, width: audioWidth, height: audioWidth)
      
        var nameW = nameContainerWidth
        //学员的
        if (self.graffitiStatus.isHidden == false) {
                nameW = nameContainerWidth - (nameContainerWidth - audioStatusX )
        }

        self.name.frame = CGRect(x: nameX, y: (self.nameContainer.frame.size.height - nameHeight)/2, width: nameW, height: nameHeight)
       
       let maskPath = UIBezierPath.init(roundedRect: self.nameContainer.bounds, byRoundingCorners:[UIRectCorner.topRight,UIRectCorner.bottomLeft], cornerRadii: CGSize(width: 4, height: 4))
        
            
            let  maskLayer = CAShapeLayer()
            maskLayer.frame = self.nameContainer.bounds
            maskLayer.path = maskPath.cgPath
            
            self.nameContainer.layer.mask = maskLayer
    }
    //音视频关闭了
    func multimediaShutdownUI(_ model:TalkfunRtcModel)->Bool
    {
        var Shutdown:Bool = false
    
        //视频关闭
        if (model.video == .closeForZHUBO || model.video == .closeForUser) {
              //音频关闭
            if (model.audio == .closeForZHUBO || model.audio == .closeForUser) {
                   Shutdown = true
               }
           
        }
        
        return Shutdown;
    }
    //双击
    @objc func doubleTapAction()
    {
       if self.delegate != nil && (self.delegate?.responds(to: #selector(TF1to16VideoAreaCellDelegate.objectDidDragged(_:))))!{
         
           self.delegate?.objectDidDragged(self)
       }

    }
    //单击
    @objc func zoomButtonClick()
    {
        
       if self.delegate != nil && (self.delegate?.responds(to: #selector(TF1to16VideoAreaCellDelegate.dealSingleTap(_:))))!{
         
           self.delegate?.dealSingleTap(self)
       }

    }
}

@objc public protocol TF1to16VideoAreaCellDelegate : NSObjectProtocol {
    //双击了
    func objectDidDragged(_ vc : TF1to16VideoAreaCell)
    //单击
    func dealSingleTap(_ vc : TF1to16VideoAreaCell)

}
